import { Component, OnInit } from '@angular/core'
import { DynamicDialogRef, DynamicDialogConfig } from 'primeng/api';
import { MessageService } from 'primeng/components/common/messageservice';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { QkProfileService } from './profile.service';
import { MdlDialogService } from '@angular-mdl/core';
import * as cfg from '../../../common/configuration';

@Component({
  selector: 'app-profile',
  templateUrl: './profile.component.html',
  styleUrls: ['./profile.component.scss'],
  providers: [MessageService, QkProfileService, MdlDialogService]
})
export class QkProfileComponent implements OnInit {
  zh = cfg.DATE_ZH_CN;
  src: string = "/assets/images/avatar.jpg"; //默认头像
  profile: any = {};
  profileForm: FormGroup;


  constructor(
    public fb: FormBuilder,
    public ref: DynamicDialogRef,
    public config: DynamicDialogConfig,
    public mdlDialogService: MdlDialogService,
    public profileService: QkProfileService) { }

  ngOnInit() {
    this.profileService.showAvatar().subscribe(data => {
      if (data != null && data != "") this.src = "data:image/jpeg;base64," + data
    });
    this.profileService.loadHumanInfo().subscribe(profile => {
      this.profile = profile;
      this.buildForm();
    });
    this.buildForm();
  }

  save() {
    let p = this.profileForm.value;
    // if(p.password && p.password.length > 0){
    //   if(p.password != p.passwordConfirm){
    //     this.mdlDialogService.alert('两次输入的密码不一致');
    //     return;
    //   }
    // }
    this.profileService.updateProfile(p).subscribe(data => {
      this.ref.close(this.src);
    })
  }

  close() {
    this.ref.close(this.src);
  }

  onFileUpload(event, file) {
    const formData: FormData = new FormData();
    formData.append('file', event.files[0], event.files[0].name);
    this.profileService.uploadAvatar(formData).subscribe(data => {
      this.src = "data:image/jpeg;base64," + data;
        file.clear();
    });
  }

  buildForm(): void {
    this.profileForm = this.fb.group({
      'humanId': [this.profile.humanId],
      'humanName': [this.profile.humanName, Validators.required],
      'identityNum': [this.profile.identityNum, Validators.required],
      'gender': [this.profile.gender],
      'birthday': [this.profile.birthday],
      'telephone': [this.profile.telephone],
      'unitName': [this.profile.unitName],
      'lawCardNum': [this.profile.lawCardNum],
      'password': [this.profile.password],
      'passwordConfirm': [this.profile.passwordConfirm]
    });
    this.profileForm.valueChanges
      .subscribe(data => this.onValueChanged(data));
    this.onValueChanged();
  }

  onValueChanged(data?: any) {
    if (!this.profileForm) { return; }
    const form = this.profileForm;
    for (const field in this.formErrors) {
      this.formErrors[field] = '';
      const control = form.get(field);
      if (control && control.dirty && !control.valid) {
        const messages = this.validationMessages[field];
        for (const key in control.errors) {
          this.formErrors[field] += messages[key] + ' ';
        }
      }
    }
  }

  formErrors = {
    'humanName': '',
    'identityNum': ''
  };

  validationMessages = {
    'humanName': {
      'required': '人员姓名为必填项'
    },
    'identityNum': {
      'required': '身份证号码为必填项'
    }
  };
}
